<?php
/**
 * 语言类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CLang.php 2010-05-23 17:12:23Z andery $
 */
class CLang
{
    private static $_lang = array();
    private static $_langer = '';
    
    //初始化 加载框架语言
    public static function initLang()
    {
        self::$_langer = CConf::get('system.lang');
        self::_set(require_once(NM_PATH.'/language/' . self::$_langer . '.php'));
        return;
    }
    
    public static function load($name)
    {
        if (strpos($name, '.')) {
            list($type, $name) = explode('.', $name);
            switch ($type) {
            	case 'lib' :
            	    $lang_file = NM_PATH.'/lib/language/' . self::$_langer . '/' . $name . '.lang.php';
            	    break;
            	case 'app' :
            	    $lang_file = Nimo::app()->getAppPath() . CConf::get('system.lang_path') . '/' . self::$_langer . '/' . $name . '.lang.php';
            	    break;
            	case 'ext' :
            	    $lang_file = Nimo::app()->getAppPath() . CConf::get('system.lang_path') . '/' . self::$_langer . '/ext/' . $name . '.lang.php';
            	    break;
            }
            if (is_file($lang_file)) {
                self::_set(include_once($lang_file));
            } else {
                return;
            }
        } else {
            return;
        }
    }
    
    /**
     * 获取语言项
     */
    public static function get($alia = '')
    {
        if ($alia=='') {
            return self::$_lang;
        }
        if (!array_key_exists($alia, self::$_lang)) {
            return $alia;
        }
        return self::$_lang[$alia];
    }
    
    /**
     * 设置语言项
     */
    private static function _set($alia, $value = '')
    {
        if (is_string($alia)) {
            self::$_lang[$alia] = $value;
        }
        if (is_array($alia)) {
            self::$_lang = array_merge(self::$_lang, $alia);
        }
    }
}